<?php
include_once  'BaseService.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/db/UserDAO.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/util/Util.php';  

class UserService extends BaseService {
	
	public function __construct(){
		parent::__construct();
	 	parent::setTarget($this);	
	 	$this->log = Plog::factory(__FILE__);	
	}
	
	public function managerLogout() {
		@session_start();
		if (isset($_SESSION["user"])) {
			 unset($_SESSION["user"]);
		}
		if (isset($_SESSION["ad"])) {
			 unset($_SESSION["ad"]);
		}  
		Util::forward("/ad/html/backoffice/backofficeLogin.php"); 	 
	}
	
	public function tigerLogout() {
		@session_start();
		if (isset($_SESSION["user"])) {
			 unset($_SESSION["user"]);
		}
		if (isset($_SESSION["ad"])) {
			 unset($_SESSION["ad"]);
		}  
		Util::forward("/ad/html/frontoffice/tigerLogin.php"); 	 
	}
	
	public function  modifyManagerAccount() {
		$account = $_GET["account"];
 		$password = $_GET["password"];
 		$modifyAccount = $_GET["modifyAccount"];
 		
 		$userDAO = new UserDAO();
 		$reuslt = $userDAO->modifyManagerAccount($account, $password, $modifyAccount);
 		echo "{result : $reuslt}";
	}
	
 	public function  modifyUser() {
 		$account = $_POST["account"];
 		$password = $_POST["password"];
 		$status = $_POST["status"]; 	
 		$userDAO = new UserDAO();
 		$reuslt = $userDAO->modifyUser($account, $password, $status);
 		echo "{result : $reuslt}";
 	}
	
	
	public function getUserAll() {
		$start = $_REQUEST["start"];
		$limit = $_REQUEST["limit"];
		$user = new UserDAO();
		$result = $user->getUserAll($start, $limit);
		echo "{count:".$result["count"].",row:".Util::toJson($result["items"])."}";
	}
	
	public function getUserNotStatus() {
		$account =  $_GET["account"];
		$userDAO = new UserDAO();
		$user = $userDAO->getUserByAccount($account, false);
		echo Util::toJson($user);
	}
	
	public function getUser() {
		$account =  $_GET["account"];
		$userDAO = new UserDAO();
		$user = $userDAO->getUserByAccount($account);
		echo Util::toJson($user);
	}
	
	
	/**
	 * 更新使用者通知設定
	 * Enter description here ...
	 */
	public function updateUserNotify() {
		
	 	@session_start();
		$user = Util::jsonToObject($_SESSION["user"]);
		
		$userDAO = new UserDAO();
		 
		$reuslt = $userDAO->updateUserNotify(
			$user->Account,
			isset($_POST["notifyBudgetUpdate"]) ? "1" :"0",
			isset($_POST["notifyAdApprovedNot"]) ? "1" :"0",
		    isset($_POST["notifyAdModify"] )? "1" :"0",
		    isset($_POST["notifyAdApproved"] )? "1" :"0",
		    isset($_POST["notifyPayComplete"])? "1" :"0"
		);
		
		if ($reuslt==1) {
			$user = $userDAO->getUser($user->Account,$user->Password);
			$_SESSION["user"] = Util::toJson($user); //更新user session
			echo "{result :1}";
		} else {
			echo "{result :0}";
		}
	}
	
	public function  queryMenagerMemeber() {
		$account = $this->getRequest("account");
		$name = $this->getRequest("name");
		$type = $this->getRequest("type");
		$isPay = $this->getRequest("isPay");
		$isNew = $this->getRequest("isNew");
		$start = $this->getRequest("start");
		$limit = $this->getRequest("limit");
		
		$userDAO =  new UserDAO();
		$result  =  $userDAO->queryMenagerMemeber($account ,$name, $type, $isPay, $isNew, $start, $limit);
		echo "{count:".$result["count"].",row:".Util::toJson($result["items"])."}";	
	}
	
	public function managerLogin() {
		$account = $_POST["account"];
		$password = $_POST['password'];
		$userDAO = new UserDAO();
		
		$user = $userDAO->getMenagerUser($account, $password);
	 	
		if ($user == null) { //無帳號
			Util::alert("帳號不存在,請重新登入!");
			Util::forward("/ad/html/backoffice/backofficeLogin.php?&account=$account&password=$password");
		} else { //一般使用者
			@session_start();
			$_SESSION["user"] = Util::toJson($user);
			Util::forward("/ad/html/backoffice/backofficeMember.php");
		} 
	}
	
	public function tigerLogin() {
		$account = $_POST["account"];
		$password = $_POST['password'];
		$userDAO = new UserDAO();
		
		$user = $userDAO->getUser($account, $password);
	 	
		if ($user == null) { //無帳號
			Util::alert("帳號不存在,請重新登入!");
			Util::forward("/ad/html/frontoffice/tigerLogin.php?&account=$account&password=$password");
		} else if ($user->Type == 0 || $user->Type ==1) { //媒體商跟管理員
			@session_start();
			$_SESSION["user"] = Util::toJson($user);
			Util::forward("/ad/html/frontoffice/tigerMyClient.php");
		} else { //一般使用者
			@session_start();
			$_SESSION["user"] = Util::toJson($user);
			Util::forward("/ad/html/frontoffice/tigerManageAd.php");
		} 
	}
	
	public function  getSettleDetailByAccount() {
		@session_start();
		$user = Util::jsonToObject($_SESSION["user"]);
		$userDAO = new UserDAO();
		$data = $userDAO->getSettilDetailByAccount($user->{"Account"});
		echo Util::toJson($data);
	}
	
	/**
	 * 代理商查詢會員資料
	 */
	public function queryTargetAccountMember() {
		@session_start();
		$start =$_POST["start"];
		$limit = $_POST["limit"];
		$user = Util::jsonToObject($_SESSION["user"]);
		$userDAO = new UserDAO();
		
		$result = $userDAO->queryTargetAccountMember($user->{"Account"}, $start, $limit);
	    
		echo "{count:".$result["count"].",row:".Util::toJson($result["items"])."}";	
	}
	
	public function checkAccount() {
		$userDAO=  new UserDAO();
		$reuslt = $userDAO->checkAccount($_GET['account']);
		echo "{success:true,result:'$reuslt'}";
	}
	
	public function getTargetAccount() {
		$userDAO = new UserDAO();
		$result = $userDAO->getTargetAccount();
		$json = Util::toJSON($result);
		echo "{success:true,result:$json}";
	}
	
	public function tigetUpdate() {
		$nowDate =  Util::getNowDate();
		$param = array(
			"account" => $_POST["account"],
			"password" =>  $_POST["password"],
			"companyName" => $_POST["companyName"],
			"name" => $_POST["name"],
		    "phone" =>$_POST["phone"],			
			"logo" => $_POST["logo"],			
			"county" => $_POST["county"],
			"area"=> $_POST["area"],
			"zipcode"=>$_POST["zipcode"],
			"address"=>$_POST["address"],
			"ModifyTime" => $nowDate 
		);
		  
		$userDAO=  new UserDAO();
		$reuslt = $userDAO->modifyUserTiget($param);
		echo "{result:'$reuslt'}";
		
	}
	
	public function managerUpdate() {
		$targetAccount = "";
			$type= $_POST["type"];
			if ($type == 2) { //一般身分
				if ($_POST["agency"] == 1) { //一般使用者有代理商
					$targetAccount = $_POST["tagetAccount"];	
				}
			}
		 
			$nowDate =  Util::getNowDate();
			$param = array(
				"account" => $_POST["account"],
				"password" =>  $_POST["password"],
				"companyName" => $_POST["companyName"],
			    "count" => $_POST["count"],
				"name" => $_POST["name"],
			    "phone" =>$_POST["phone"],
				"type"=> $type,
				"proxyAccount" => $targetAccount,
				"logo" => $_POST["logo"],
				"status" => "1",
				"county" => $_POST["county"],
				"area"=> $_POST["area"],
				"zipcode"=>$_POST["zipcode"],
				"address"=>$_POST["address"],
				"ModifyTime" => $nowDate 
			);
			  
			$userDAO=  new UserDAO();
			$reuslt = $userDAO->modifyUserManager($param);
			echo "{result:'$reuslt'}";
	}
	
	/**
	 * 管理者新增使用者	 
	 */
	public function managerInsert() {
		try {
			$targetAccount = "";
			$type= $_POST["type"];
			if ($type == 2) { //一般身分
				if ($_POST["agency"] == 1) { //一般使用者有代理商
					$targetAccount = $_POST["tagetAccount"];	
				}
			}
			
			$nowDate =  Util::getNowDate();
			$param = array(
				"account" => $_POST["account"],
				"password" =>  $_POST["password"],
				"companyName" => $_POST["companyName"],
			    "count" => $_POST["count"],
				"name" => $_POST["name"],
			    "phone" =>$_POST["phone"],
				"type"=> $type,
				"logo" => $_POST["logo"],
				"proxyAccount" => $targetAccount,
				"status" => "1",
				"county" => $_POST["county"],
				"area"=> $_POST["area"],
				"zipcode"=>$_POST["zipcode"],
				"address"=>$_POST["address"],
				"createTime"=>$nowDate,
				"ModifyTime" => $nowDate 
			);
			 	
			$userDAO=  new UserDAO();
			$reuslt = $userDAO->insert($param);
			
			if ($reuslt == 1) {
				$obj = (Object) $param;
				$fdate = Util::getNowDate('Y/m/d');
				Util::email(
					$obj->account, 
					"Amazing Mobile ADs通知：會員$obj->name 已完帳號建立，請您立即啟用", 
					"親愛的 $obj->name 您好，<BR>

					   感謝您加入Amazing Mobile Ads廣告，您的會員資料已於<font color=red> $fdate </font> 建立產生<BR>
					 <b>您的帳號為:</b> $obj->account <BR>
					 <b>您的密碼為:$obj->password </b><BR>
					  歡迎您至<a href='http://mmedia.ai-ad.net.tw/ad/html/frontoffice/tigerLogin.php'>http://mmedia.ai-ad.net.tw/ad/html/frontoffice/tigerLogin.php</a> 登錄您的宣傳廣告 <BR>
					  如果您對於廣告上有任何問題，請來信至: service@ai-ad.net.tw <BR>
					<BR>
					  謝謝您，<BR>
					 Amazing Mobile Ads敬上<BR>
					"
				);
			}
			
		 	$msg = $reuslt =="" ? "帳戶已被註冊!":"";
			echo "{success:true,result:'$reuslt',msg :'$msg'}";
		} catch(Exception $e) {
			echo "{failure :true,result:'0',msg:'$e->getMessage()'}";
		}
	}
	
	/**
	 * 
	 * 媒體商 新增使用者
	 */
	public function trgetInsert(){
		try {
			@session_start();
			$user = Util::jsonToObject($_SESSION["user"]);
			 
			$nowDate =  Util::getNowDate();
			$param = array(
				"account" => $_POST["account"],
				"password" =>  $_POST["password"],
				"companyName" => $_POST["companyName"],
			    "count" => 0,
				"name" => $_POST["name"],
			    "phone" =>$_POST["phone"],
				"logo" => $_POST["logo"],
				"proxyAccount" => $user->{"Account"},
				"status" => "1",
				"county" => $_POST["county"],
				"area"=> $_POST["area"],
				"zipcode"=>$_POST["zipcode"],
				"address"=>$_POST["address"],
				"createTime"=>$nowDate,
				"ModifyTime" => $nowDate 
			);
			 	 
			$userDAO=  new UserDAO();
			$reuslt = $userDAO->insert($param);
		 	
			if ($reuslt == 1) {
				$obj = (Object) $param;
				$fdate = Util::getNowDate('Y/m/d');
				Util::email(
					$obj->account, 
					"Amazing Mobile ADs通知：會員$obj->name 已完帳號建立，請您立即啟用", 
					"親愛的 $obj->name 您好，<BR>

					   感謝您加入Amazing Mobile Ads廣告，您的會員資料已於<font color=red> $fdate </font> 建立產生<BR>
					 <b>您的帳號為:</b> $obj->account <BR>
					 <b>您的密碼為:$obj->password </b><BR>
					  歡迎您至<a href='http://mmedia.ai-ad.net.tw/ad/html/frontoffice/tigerLogin.php'>http://mmedia.ai-ad.net.tw/ad/html/frontoffice/tigerLogin.php</a> 登錄您的宣傳廣告 <BR>
					  如果您對於廣告上有任何問題，請來信至: service@ai-ad.net.tw <BR>
					<BR>
					  謝謝您，<BR>
					 Amazing Mobile Ads敬上<BR>
					"
				);
			}
			
			$msg = $reuslt =="" ? "帳戶已被註冊!":"";
	 
			echo "{success:true,result:'$reuslt',msg :'$msg'}";
		} catch(Exception $e) {
			echo "{failure :true,result:'0', msg:'$e->getMessage()'}";
		}
	} 
}

$service =  new UserService();
$service->__call($_GET["action"], null);
//$service->queryTargetAccountMember();
?>